/* file: https://fm.cnbc.com/applications/cnbc.com/resources/files/2020/11/25/dianomi-fix.js?vn=1606329033*/
document.querySelectorAll('.dianomi_context[data-dianomi-context-id="204"]')[1].remove();
/* file: https://fm.cnbc.com/applications/cnbc.com/resources/files/2018/08/10/updateSavedSymbolsExpiry.js?vn=1533930678*/
window.postLoadFunctions = window.postLoadFunctions || {};
window.postLoadFunctions['updateSavedSymbolsExpiry'] = function() {

var theDate = new Date();
var oneYearLater = new Date( theDate.getTime() + 31536000000 );
var expiryDate = oneYearLater.toGMTString();

function savedSymbolsUpdate(symbols, limit) {

		var limit = limit || 25;
		var	savedViewedSymbols = (CNBC_Utils.readSavedSymbols().length > 0) ? CNBC_Utils.readSavedSymbols() : [];
		var mergedArray = removeDuplicates(savedViewedSymbols);
		if(mergedArray.length > limit) {
			mergedArray.length = limit;
		};

		function removeDuplicates(array) {
			console.log("remove dupes is called")
			var tempObj= {};
			var tempArray = [];
			for(var i=0; i < array.length; i++) {
				tempObj[array[i]['symbolName']] = {'symbolName' : array[i]['symbolName'], 'companyName' : array[i]['companyName'], 'countryCode' : array[i]['countryCode']};
			};
			for(var item in tempObj) {
				if(tempObj.hasOwnProperty(item)) {
					tempArray.push(tempObj[item])
				};
			};
			return tempArray;
		};

		CNBC_Utils.createCookie('savedViewedSymbols', JSON.stringify(mergedArray), 43800);

	};
  savedSymbolsUpdate();
}

/* file: https://fm.cnbc.com/applications/cnbc.com/resources/files/2018/01/25/quote-synthetic-fix.js?vn=1516894280*/
window.postLoadFunctions = window.postLoadFunctions || {}; window.postLoadFunctions['quotesyntheticfix'] = function() {
  if(symbolInfo.assetSubType== "CNBC Synthetic"){
    $("span:contains('52 week range'), .year-container").hide();
  }
}

/* file: https://fm.cnbc.com/applications/cnbc.com/resources/files/2017/06/09/change-font.js?vn=1497039935*/
if($('span.symbol').text() != "BRK.A"){
	$('.change span.sign-f, .change span.sign-t').css('font-size','28px')
	$('.change').css('padding-left','20px')
}
/* file: https://fm.cnbc.com/applications/cnbc.com/resources/files/2018/05/18/alerts_0.js?vn=1526669158*/
window.postLoadFunctions = window.postLoadFunctions || {}; window.postLoadFunctions['quotebreakingnews'] = function() {
$.ajax({
    url: 'https://www.cnbc.com/services/alerts',
    error: function() {
      $('.cnbc-alert-container-breaking-news').addClass('hide');
      setInterval(bNCall, 30000);

      function bNCall() {
        $.ajax({
            url: 'https://www.cnbc.com/services/alerts',
            error: function() {
              $('#cnbc-alert-container-breaking-news').addClass('hide');
              setInterval(bNCall, 30000);
            },
            success: function(result){
          //  console.log(result);
            if(result.alerts['breaking-news']){
              //  console.log("we have alert: " + result.alerts['breaking-news'][0].url);
                var breakingNewsBanner = '<div id="cnbc-alert-container-breaking-news" class="news-alert breaking-news" style="display: block; margin-bottom:5px;"><div class="content"><div class="news-alert-container"><div class="news-alert-title"><h2 class="headline"><a class="alert-banner" id="breaking-news-link" data-nodeid="'+result.alerts['breaking-news'][0].nid+'" data-regionid="false" href="'+result.alerts['breaking-news'][0].url+'"><span class="alert-title" style="margin-right:5px;">BREAKING:</span><span class="alert-headline">'+result.alerts['breaking-news'][0].headline+'</span></a></h2></div><div class="alert-close-button" id="breaking-news-close-button">×</div><div style="clear:both;"></div></div></div></div>';
                $('#cnbc-alert-container-breaking-news').remove();
                $('#cnbc-new-header').after(breakingNewsBanner);
              }else{
                $('#cnbc-alert-container-breaking-news').addClass('hide');
                setInterval(bNCall, 30000);
              }
            }
          });
        }

    },
    success: function(result){
  //  console.log(result);
    if(result.alerts['breaking-news']){
      //  console.log("we have alert: " + result.alerts['breaking-news'][0].url);

        $('#cnbc-new-header').after('<div id="cnbc-alert-container-breaking-news" class="news-alert breaking-news" style="display: block; margin-bottom:5px;"><div class="content"><div class="news-alert-container"><div class="news-alert-title"><h2 class="headline"><a class="alert-banner" id="breaking-news-link" data-nodeid="'+result.alerts['breaking-news'][0].nid+'" data-regionid="false" href="'+result.alerts['breaking-news'][0].url+'"><span class="alert-title" style="margin-right:5px;">BREAKING:</span><span class="alert-headline">'+result.alerts['breaking-news'][0].headline+'</span></a></h2></div><div class="alert-close-button" id="breaking-news-close-button">×</div><div style="clear:both;"></div></div></div></div>');
        setInterval(bNCall, 30000);

    }else{
      $('#cnbc-alert-container-breaking-news').addClass('hide');
      setInterval(bNCall, 30000);
    }
        function bNCall() {
          $.ajax({
              url: 'https://www.cnbc.com/services/alerts',
              error: function() {
                $('#cnbc-alert-container-breaking-news').addClass('hide');
                setInterval(bNCall, 30000);
              },
              success: function(result){
            //  console.log(result);
              if(result.alerts['breaking-news']){
              //    console.log("we have alert: " + result.alerts['breaking-news'][0].url);
                  var breakingNewsBanner = '<div id="cnbc-alert-container-breaking-news" class="news-alert breaking-news" style="display: block; margin-bottom:5px;"><div class="content"><div class="news-alert-container"><div class="news-alert-title"><h2 class="headline"><a class="alert-banner" id="breaking-news-link" data-nodeid="'+result.alerts['breaking-news'][0].nid+'" data-regionid="false" href="'+result.alerts['breaking-news'][0].url+'"><span class="alert-title" style="margin-right:5px;">BREAKING:</span><span class="alert-headline">'+result.alerts['breaking-news'][0].headline+'</span></a></h2></div><div class="alert-close-button" id="breaking-news-close-button">×</div><div style="clear:both;"></div></div></div></div>';
                  $('#cnbc-alert-container-breaking-news').remove();
                  $('#cnbc-new-header').after(breakingNewsBanner);
                }else{
                $('#cnbc-alert-container-breaking-news').addClass('hide');
                // setInterval(bNCall, 30000);
              }
              }
            });
          }
      }
  });


  $('body').on('click', '#breaking-news-close-button', function() {
    $('#cnbc-alert-container-breaking-news').remove();
});

};

/* file: https://fm.cnbc.com/applications/cnbc.com/resources/files/2019/03/01/events-skin.js?vn=1551472741*/
window.postLoadFunctions = window.postLoadFunctions || {}; window.postLoadFunctions['quoteEventsAlerts'] = function() {
	//GET DATA FROM symbolInfo
	var splitExDate = symbolInfo.EventData.split_ex_date_today;
	var divExEarningsDate = symbolInfo.EventData.div_ex_date_today;
	var nextEarningsDateToday = symbolInfo.EventData.next_earnings_date_today;
	var yrhiind =symbolInfo.EventData.yrhiind;
	var yrloind =symbolInfo.EventData.yrloind;
	var announceTime = symbolInfo.EventData.announce_time;

	//MOCK DATA(for testing)
	//var divExEarningsDate = "Y";
	//var nextEarningsDateToday = "Y";
	// var yrhiind = "N";
	//var yrloind = "Y";
	//var splitExDate = "Y";

	var alertMessage;
	var answerClass;
	var answerClassArray = [];
	var eventMessageArray = [];
	var eventsData ={
		"item": [{
				"event": nextEarningsDateToday,
				"eventMessage": "Earnings Announcement Today",
	      "class": "nextEarningsDateToday"
			},
			{
				"event": divExEarningsDate,
				"eventMessage": "Stock is Ex-Dividend Today",
	      "class": "divExEarningsDate"
			},
			{
				"event": yrhiind,
				"eventMessage": "New 52 Week High Today",
	      "class": "yrhiind"
			},
			{
				"event": yrloind,
				"eventMessage": "New 52 Week Low Today",
	      "class": "yrloind"
			},
			{
				"event": splitExDate,
				"eventMessage": "Split/Adjustment Today",
	      "class": "splitExDate"
			}
		]
	};

	// If <announce_time>= "A", text should be: "Earnings Announcement After Market Close"
	// If <announce_time>= "B", text should be: "Earnings Announcement Before Market Open"
	// If <announce_time>= "D", text should be: "Earnings Announcement Today"
	// If <announce_time>=NULL, text should be: "Earnings Announcement Today"

	$.each( eventsData, function( key, value ) {
	  $.each( value, function( keys, values ) {
	    if(values.event === "Y"){
				console.log("VE: " + values.event + "val only: " + values)
				if(values.class == "nextEarningsDateToday"){
					console.log("announce time :" + announceTime);
					switch(announceTime) {
						case "A":
						values.eventMessage = "Earnings Announcement After Market Close";
						break;
						case "B":
						values.eventMessage = "Earnings Announcement Before Market Open";
						break;
						case "D":
						values.eventMessage = "Earnings Announcement Today";
						break;
						default:
						values.eventMessage = "Earnings Announcement Today";
					}
				}

	      answerClassArray.push(values.class);
	      eventMessageArray.push(values.eventMessage);
	      // DY.API('event',{name: 'QuoteAlert-' + values.class + '-Impression', properties:{}});
	      // console.log('QuoteAlert-' + values.class + '-Impression')
	    }
	  });
	  answerClass = answerClassArray.join(' ');
	  alertMessage = eventMessageArray.join('<br><i class="fa fa-bell" aria-hidden="true"></i>');
	});


	var alertsMarkup = `
	<style>
	body .quote-events-alert{background:#fdea5a;font-size:16px;color:#333746;padding:5px 10px;margin-top:10px;}
	.feedback-question{float:right;font-size:12px;margin-top:3px;}
	a.feedback-answer{margin-left:3px;color:#616061;}
	a.feedback-answer:hover{color:#616061;text-decoration:underline;}
	.quote-rightrail{margin-top:10px;}
	.event-details{line-height:24px;}
	.mobile-touch .feedback-question{margin-top:-3px;width:65px;text-align:center;}
	.quote-events-alert .fa{margin-right:5px;}
	.mobile-touch .quote-events-alert .fa {margin-right: 5px;margin-bottom: 14px;float: left;margin-top: 4px;}
	.mobile-touch .message{display:block;}
	</style>
	<div class="quote-events-alert">
	  <span class="event-details"><i class="fa fa-bell" aria-hidden="true"></i><span class="message">`+alertMessage+`</span></span>
	</div>`;


	if(alertMessage.length){
		$('.quote-section-header').prepend(alertsMarkup);
		$('.quote-events-alert').hide();
		setTimeout(function(){ $('.quote-events-alert').slideDown('fast'); }, 2000);
	}
}

